package net.system.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.handsome.ip.IPSeeker;

import net.core.common.StrUtils;
import net.system.base.bean.SSysuser;
import net.trade.common.Constants;

public class HttpUtils {

	/**
	 * 获取当前登陆用户信息
	 *
	 * @param request
	 * @return UserInfor 登陆用户信息
	 */
	public static SSysuser getUserInfo(HttpServletRequest request){
		SSysuser userInfo = null;
		if(request != null){
			HttpSession session = request.getSession(false);
			if(session != null){
				userInfo = (SSysuser) session.getAttribute(Common.USER_SESSION);
			}
		}
		return userInfo;
	}
	
	/**
	 * 获取当前登陆用户的姓名
	 *
	 * @param request
	 * @return String 当前登陆用户姓名
	 */
	public static String getCurrentLoginUserName(HttpServletRequest request){
		String username = "";
		SSysuser userInfo = getUserInfo(request);
		if(userInfo != null){
			username = StrUtils.getNotNullStrValue(userInfo.getSuName());
		}
		return username;
	}
	
	/**
	 * 获取当前登陆用户的ID
	 *
	 * @param request
	 * @return int 当前登陆用户ID
	 */
	public static String getCurrentLoginUserId(HttpServletRequest request){
		String userId = "";
		SSysuser userInfo = getUserInfo(request);
		if(userInfo != null){
			userId = StrUtils.getNotNullStrValue(userInfo.getRecId());
		}
		return userId;
	}
	
	/**
	 * 
	 * Description: 获得用户当前操作模块
	 * @Title: getCurrentLoginUserModule
	 * @param request
	 * @return String   
	 *
	 */
	public static String getCurrentLoginUserModule(HttpServletRequest request){
		String module = "";
		if(request != null){
			HttpSession session = request.getSession(false);
			if(session != null){
				module = session.getAttribute(Common.USER_SESSION)+"";
			}
		}
		if("".equals(module)){
			module = "";
		}
		return StrUtils.getNotNullStrValue(module);
	}
	
	/**
	 * 
	 * Description: 获得客户端访问ip地址
	 * @Title: getIpAddr
	 * @param request
	 * @return String   
	 */
	public static String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    String iplist[] = ip.split(",");
	    ip = iplist[0];
	    return ip;  
	} 
	/**
	 * 
	 * Description: 获得客户端访问ip地址 所在区域
	 * @Title: getIpArea
	 * @param ipAddr
	 * @return String   
	 */
	public static String getIpArea(String ipAddr,String dir) {
		String address = "";
		try{
			//指定纯真数据库的文件名，所在文件夹
			IPSeeker ip=new IPSeeker("IPAREA.Dat",dir);
			//获得地区
			address = ip.getIPLocation(ipAddr).getCountry();
			//System.out.println(address);
		}catch (Exception e) {
			e.printStackTrace();
	    	return ipAddr;
		}
	    return address;
	}
	/**
	 * 
	 * Description: 获得客户端访问ip地址 类型（:网通电信等）
	 * @Title: getIpType
	 * @param ipAddr
	 * @return String   
	 */
	public static String getIpType(String ipAddr,String dir) {
		//指定纯真数据库的文件名，所在文件夹
	    IPSeeker ip=new IPSeeker("IPAREA.Dat",dir);
	    //获得类型:网通电信等
	    String type = ip.getIPLocation(ipAddr).getArea();
	    //System.out.println(type);
	    return type;
	} 
	
}
